const {usersModel}=require('./models/usersModel');
module.exports.getUsers= async (msg)=>{
    // const data=await getUsers(msg);
    console.log(msg);
    let { pageSize, currentPage, value = "" ,salespersonsId} = msg;
    let total = await usersModel.countDocuments({
        isDel: false,
        name: { $regex: value, $options: '$i' },
        salespersonsId
    });
    let pages = Math.ceil(total / pageSize);
    let users = await usersModel
        .find({
            isDel: false,
            name: { $regex: value, $options: '$i' },
            salespersonsId
        })
        // .sort({stuAge:1})
        .populate({
            path: "salespersonsId",
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
        // console.log(total,pages,users);



        let alltotal = await usersModel.countDocuments({
            isDel: false,
            name: { $regex: value, $options: '$i' },
        });
        let alluser=await usersModel.find({
            isDel: false,
            name: { $regex: value, $options: '$i' }
        })
        // .sort({stuAge:1})
        .populate({
            path: "salespersonsId",
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);
        console.log(total, pages, users,alltotal,alluser);
        return { total, pages, users,alltotal,alluser }
}
module.exports.addUsers= async (msg)=>{
    const data=await usersModel.create({...msg});
    return data;
}
module.exports.getUsersById= async (msg)=>{
    // console.log(msg);
    const data=await usersModel.find({_id:msg._id});
    return data;
}
module.exports.updateUsers= async (msg)=>{
    // console.log(msg);
    const data=await usersModel.updateOne({_id:msg._id},{name: msg.name,
    phone: msg.phone});
    return data;
}
module.exports.deleteUsersById= async (msg)=>{
    const data=await usersModel.updateOne({_id:msg._id},{isDel:true});
    return data;
}